﻿using System;
using System.Collections.Generic;
using System.Text;
using DGMResourcesTool.Logic.FileUtil;

namespace DGMResourcesTool.Logic.Config
{
    public class SuffixConfigUtility
    {
        private static bool _isSuffixInit = false;
        private static readonly List<string> SuffixList = new List<string>();
        private static readonly List<string> PrefixList = new List<string>();

        /// <summary>
        /// 强制重新加载.
        /// </summary>
        public static void Reload()
        {
            Init(true);
        }

        private static void Init(bool isForceReaload = false)
        {
            if (_isSuffixInit && !isForceReaload)
            {
                return;
            }

            SuffixList.Clear();
            PrefixList.Clear();
            string suffixFilePath = FileUtility.GetConfigFilePath("suffix.txt");
            string[] lines = FileReaderWriterUtility.ReadFile(suffixFilePath, Encoding.UTF8);
            foreach (var line in lines)
            {
                string[] tempStrs = line.Split('#');
                if (tempStrs.Length == 2)
                {
                    // 配置文件中无注释.
                    if (Int32.Parse(tempStrs[1]) == 0)
                    {
                        PrefixList.Add(tempStrs[0]);
                    }
                    else
                    {
                        SuffixList.Add(tempStrs[0]);
                    }
                }
                else if (tempStrs.Length == 3)
                {
                    // 配置文件中有注释.
                    if (Int32.Parse(tempStrs[2]) == 0)
                    {
                        PrefixList.Add(tempStrs[1]);
                    }
                    else
                    {
                        SuffixList.Add(tempStrs[1]);
                    }
                }
            }

            _isSuffixInit = true;
        }

        /// <summary>
        /// 检测有无后缀,如果有后缀则删除后缀.
        /// </summary>
        public static string DeleteSuffix(string str)
        {
            Init();
            string matchSuffix = string.Empty;
            string matchPrefix = string.Empty;
            string result = str;
            foreach (var suffixStr in SuffixList)
            {
                if (str.EndsWith(suffixStr))
                {
                    // 检测能匹配到的最长的字符串.
                    if (string.IsNullOrEmpty(matchSuffix) || suffixStr.Contains(matchSuffix))
                    {
                        matchSuffix = suffixStr;
                        result = str.Remove(str.Length - suffixStr.Length, suffixStr.Length);
                    }
                }
            }

            str = result;
            foreach (var prefixStr in PrefixList)
            {
                if (str.StartsWith(prefixStr))
                {
                    // 检测能匹配到的最长的字符串.
                    if (string.IsNullOrEmpty(matchPrefix) || prefixStr.Contains(matchPrefix))
                    {
                        matchPrefix = prefixStr;
                        result = str.Remove(0, prefixStr.Length);
                    }
                }
            }

            return result;
        }
    }
}